<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;
use App\JsonRpc\CouponService;
use App\Middleware\AuthMiddleware;
use App\Request\CreateRequest;
use App\Request\UpdateRequest;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use App\Controller\BaseController;

#[Controller(prefix:"/",server:"http")]
class IndexController extends BaseController
{
    #[Inject]
    protected CouponService $service;

    /**
     * 新增优惠券数据
     */
    #[PostMapping(path: "v1")]
    #[Middleware(AuthMiddleware::class)]
    public function create(CreateRequest $request)
    {
        $data = $request->validated();
        if($this->service->create($data)){
            return $this->success(0,"新增优惠券信息成功");
        }
        return $this->fail("新增优惠券信息失败");
    }

    /**
     * 修改优惠券数据
     */
    #[PutMapping(path: "v1/{id}")]
    #[Middleware(AuthMiddleware::class)]
    public function update(UpdateRequest $request)
    {
        // 获取请求数据
        $data = $request->validated();
        // 获取ID
        $id = $request->route("id");
        if($this->service->update(trim(urldecode($id)),$data)){
            return $this->success(0,"保存优惠券信息成功");
        }
        return $this->fail("保存优惠券信息失败");
    }

    /**
     * 上下架优惠券数据
     */
    #[PutMapping(path: "v1/status/{id}")]
    #[Middleware(AuthMiddleware::class)]
    public function chanage_status()
    {
        $id = $this->request->route("id");
        $result = $this->service->check_status(trim(urldecode($id)));
        if($result["code"] == 1){
            return $this->success(0,"修改成功",$result["result"]);
        }
        return $this->fail("修改失败");
    }

    /**
     * 删除优惠券
     */
    #[DeleteMapping(path: "v1/{id}")]
    #[Middleware(AuthMiddleware::class)]
    public function delete()
    {
        $id = $this->request->route("id");
        if($this->service->del(trim(urldecode($id)))){
            return $this->success(0,"删除成功");
        }
        return $this->fail("删除失败");
    }

    /**
     * 领取优惠券
     */
    #[PostMapping(path: "v1/receive")]
    #[Middleware(AuthMiddleware::class)]
    public function receive()
    {
        $id = $this->request->post("id",'');
        if(empty($id)){
            return $this->fail("请求参数错误，必须传优惠券ID");
        }
        if($this->service->receive(trim($id))){
            return $this->success(0,"领取成功");
        }
        return $this->fail("领取失败");
    }

    /**
     * 获取优惠券详情
     */
    #[GetMapping(path: "v1")]
    #[Middleware(AuthMiddleware::class)]
    public function info()
    {
        // 获取请求数据
        $params = $this->request->getQueryParams();
        $is_user = isset($params["is_user"]) ? (int)$params["is_user"] : -1;
        $id = isset($params["id"]) ? trim($params["id"]) : '';
        // 验证数据
        if(!in_array($is_user,[0,1])){
            return $this->fail("请求参数错误，is_user只能是0或者1");
        }
        if(empty($id)){
            return $this->fail("请求参数错误，id必传");
        }
        $result = $this->service->info(trim(urldecode($id)),$is_user);
        return $this->success(0,"获取信息成功",$result);
    }

    /**
     * 获取优惠券列表
     */
    #[GetMapping(path: "v1/coupons/common")]
    #[Middleware(AuthMiddleware::class)]
    public function list_common()
    {

    }

    /**
     * 用户收藏/取消收藏优惠券
     */
    #[PostMapping(path: "v1/collection")]
    #[Middleware(AuthMiddleware::class)]
    public function collection()
    {
        $id = $this->request->post("id",'');
        if(empty($id)){
            return $this->fail("请求参数错误，必须传优惠券ID");
        }
        // 执行收藏/取消收藏
        $result = $this->service->collection(trim($id));
        $msg = $result["status"] == 0 ? "取消收藏" : "收藏";
        if($result["code"]){
            return $this->success(0,$msg."成功",["status" => $result["status"]]);
        }
        return $this->fail($msg."失败");
    }

}
